package wifiunion.com.notice.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceActivity;
import android.text.TextUtils;
import android.util.Base64;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

import wifiunion.com.notice.Constants;
import wifiunion.com.notice.entity.Param;
import wifiunion.com.notice.entity.User;

/**
 * 
 * @ClassName SharedPreferencesUtils 
 * @Description TODO 
 * @Version 
 * @Author wifiunion
 * @Creation unknow
 * @Mender wifiunion
 * @Modification 2016年7月5日 上午10:16:30 
*
 */
public class SharedPreferencesUtils {
	/**
	 * 获取配置文件中的属性值
	 */
	public static String getSharedPreferences(String propertyName,
			Context context) {
		if (context == null) {
			return "";
		}
		return context.getSharedPreferences(Constants.CONFIG_FILENAME,
				PreferenceActivity.MODE_PRIVATE).getString(propertyName, "");
	}

	/**
	 * 保存配置文件中的属性值
	 */
	public static boolean setSharedPreferences(String propertyName,
			String propertyValue, Context context) {
		if (context == null) {
			return false;
		}
		return context
				.getSharedPreferences(Constants.CONFIG_FILENAME,
						Context.MODE_PRIVATE).edit()
				.putString(propertyName, propertyValue).commit();
	}


	/**
	 *
	 * @Name getUserSharedPreferences
	 *
	 */
	public static User getUserSharedPreferences(Context context) {
		if (context == null) {
			return null;
		}
		SharedPreferences sp = context.getSharedPreferences(Constants.CONFIG_FILENAME, Context.MODE_PRIVATE);
		if (sp.contains(Constants.SP_SAVE_USER_NAME)) {
			String userString = sp.getString(Constants.SP_SAVE_USER_NAME, null);
			if(TextUtils.isEmpty(userString))
			{
				return null;
			}
			byte[] buffer = Base64.decode(userString, Base64.DEFAULT);
			ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
			ObjectInputStream ois = null;
			try {
				ois = new ObjectInputStream(bais);
				User user = (User) ois.readObject();
				return user;
			} catch (StreamCorruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} finally {
				try {
					if (bais != null) {
						bais.close();
					}
					if (ois != null) {
						ois.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}


	/**
	 *
	 * filter切换场景获取保存的user
	 *
	 */
	public static User getFilterUserSharedPreferences(Context context) {
		if (context == null) {
			return null;
		}
		SharedPreferences sp = context.getSharedPreferences(Constants.CONFIG_FILENAME, Context.MODE_PRIVATE);
		if (sp.contains(Constants.SP_SAVE_FILTERUSER_NAME)) {
			String userString = sp.getString(Constants.SP_SAVE_FILTERUSER_NAME, null);
			if(TextUtils.isEmpty(userString))
			{
				return null;
			}
			byte[] buffer = Base64.decode(userString, Base64.DEFAULT);
			ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
			ObjectInputStream ois = null;
			try {
				ois = new ObjectInputStream(bais);
				User user = (User) ois.readObject();
				return user;
			} catch (StreamCorruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} finally {
				try {
					if (bais != null) {
						bais.close();
					}
					if (ois != null) {
						ois.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}



	/**
	 *
	 * 首页登录场景获取保存的user
	 *
	 */
	public static User getPlaceUserSharedPreferences(Context context) {
		if (context == null) {
			return null;
		}
		SharedPreferences sp = context.getSharedPreferences(Constants.CONFIG_FILENAME, Context.MODE_PRIVATE);
		if (sp.contains(Constants.SP_SAVE_PLACEUSER_NAME)) {
			String userString = sp.getString(Constants.SP_SAVE_PLACEUSER_NAME, null);
			if(TextUtils.isEmpty(userString))
			{
				return null;
			}
			byte[] buffer = Base64.decode(userString, Base64.DEFAULT);
			ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
			ObjectInputStream ois = null;
			try {
				ois = new ObjectInputStream(bais);
				User user = (User) ois.readObject();
				return user;
			} catch (StreamCorruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} finally {
				try {
					if (bais != null) {
						bais.close();
					}
					if (ois != null) {
						ois.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}




	/**
	 *
	 * @Name setUserSharedPreferences
	 * @Description 存储user对象 之前存储在application 中的
	 * @param user
	 * @param context
	 * @return void
	 * @Author wifiunion
	 * @Date 2016年6月20日 上午9:53:58
	 *
	 */
	public static void setUserSharedPreferences(User user, Context context) {
		SharedPreferences sp = context.getSharedPreferences(Constants.CONFIG_FILENAME, Context.MODE_PRIVATE);

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream out = null;
		try {
			Editor editor = sp.edit();
			if (user != null) {
				out = new ObjectOutputStream(baos);
				out.writeObject(user);
				String userString = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
				editor.putString(Constants.SP_SAVE_USER_NAME, userString);
			} else {
				editor.putString(Constants.SP_SAVE_USER_NAME, "");
			}
			editor.commit();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (baos != null) {
					baos.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 *
	 * @Name
	 * @Description 存储user对象 之前存储在application 中的
	 * @param context
	 * @return void
	 * @Author wifiunion
	 * @Date 2016年6月20日 上午9:53:58
	 *
	 */
	public static Param getParamSharedPreferences(Context context) {
		if (context == null) {
			return null;
		}
		SharedPreferences sp = context.getSharedPreferences(Constants.CONFIG_FILENAME, Context.MODE_PRIVATE);
		if (sp.contains(Constants.SP_SAVE_PARAM_NAME)) {
			String userString = sp.getString(Constants.SP_SAVE_PARAM_NAME, null);
			if(TextUtils.isEmpty(userString))
			{
				return null;
			}
			byte[] buffer = Base64.decode(userString, Base64.DEFAULT);
			ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
			ObjectInputStream ois = null;
			try {
				ois = new ObjectInputStream(bais);
				Param user = (Param) ois.readObject();
				return user;
			} catch (StreamCorruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} finally {
				try {
					if (bais != null) {
						bais.close();
					}
					if (ois != null) {
						ois.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}



	public static void setParamSharedPreferences(Param param, Context context) {
		SharedPreferences sp = context.getSharedPreferences(Constants.CONFIG_FILENAME, Context.MODE_PRIVATE);

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream out = null;
		try {
			Editor editor = sp.edit();
			if (param != null) {
				out = new ObjectOutputStream(baos);
				out.writeObject(param);
				String userString = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
				editor.putString(Constants.SP_SAVE_PARAM_NAME, userString);
			} else {
				editor.putString(Constants.SP_SAVE_PARAM_NAME, "");
			}
			editor.commit();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (baos != null) {
					baos.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public static void setFilterUserSharedPreferences(User user, Context context) {
		SharedPreferences sp = context.getSharedPreferences(Constants.CONFIG_FILENAME, Context.MODE_PRIVATE);

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream out = null;
		try {
			Editor editor = sp.edit();
			if (user != null) {
				out = new ObjectOutputStream(baos);
				out.writeObject(user);
				String userString = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
				editor.putString(Constants.SP_SAVE_FILTERUSER_NAME, userString);
			} else {
				editor.putString(Constants.SP_SAVE_FILTERUSER_NAME, "");
			}
			editor.commit();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (baos != null) {
					baos.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}




	/**
	 *
	 * @Name
	 * @Description 存储user对象 之前存储在application 中的
	 * @param user
	 * @param context
	 * @return void
	 * @Author wifiunion
	 * @Date 2016年6月20日 上午9:53:58
	 *
	 */
	public static void setPlaceUserSharedPreferences(User user, Context context) {
		SharedPreferences sp = context.getSharedPreferences(Constants.CONFIG_FILENAME, Context.MODE_PRIVATE);

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream out = null;
		try {
			Editor editor = sp.edit();
			if (user != null) {
				out = new ObjectOutputStream(baos);
				out.writeObject(user);
				String userString = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
				editor.putString(Constants.SP_SAVE_PLACEUSER_NAME, userString);
			} else {
				editor.putString(Constants.SP_SAVE_PLACEUSER_NAME, "");
			}
			editor.commit();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (baos != null) {
					baos.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}



	/**
	 * 保存配置文件中的属性值
	 */
	public static boolean setBooleanSharedPreferences(String propertyName, boolean propertyValue, Context context) {
		if (context == null) {
			return false;
		}
		return context.getSharedPreferences(Constants.CONFIG_FILENAME, Context.MODE_PRIVATE).edit()
				.putBoolean(propertyName, propertyValue).commit();

	}

	/**
	 * 获取配置文件中的属性值
	 */
	public static boolean getBooleanSharedPreferences(String propertyName, Context context) {
		if (context == null) {
			return false;
		}
		return context.getSharedPreferences(Constants.CONFIG_FILENAME, PreferenceActivity.MODE_PRIVATE)
				.getBoolean(propertyName, false);
	}

}
